package com.excel.read;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlparser.tags.TableColumn;

import com.excel.CELL;
import com.excel.TableModel;
import com.excel.model.AttributeExcelModel;
import com.excel.model.ExcelModelIFace;
import com.excel.model.TableExcelModel;
import com.mike.excel.TestExcelTable;

import jxl.Cell;
import jxl.Range;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.read.biff.BlankCell;

/**
 * 得到Excel的原始表格信息，包括表格名，表格的二维数据，以及描述 用于子类的进一步处理
 * 
 * @author mike
 * 
 */
public  class ExcelTableConfig extends ExcelConfig {

	protected TableExcelModel aTableExcelModel;
	protected TableModel aTableModel;
	public String tableConfig(String realPath) {
		File excel = null;
		Workbook w = null;
		try {
			excel = new File(realPath);
			w = Workbook.getWorkbook(excel);			
			Sheet s = w.getSheet("查询");
			getMulCell(s);
			try {
				writeAllTable("", s);
			} catch (SameNameException e) {
			}
		} catch (Exception e) {

			System.out.println(e);
		} finally {
			w.close();
		}
		return "pmb";
	}

	private static boolean isTableRow(Sheet s, int curRowIndex, int cols) {
		boolean flag = false;
		for (int i = 1; i < cols; i++) {
			Cell cell = s.getCell(i, curRowIndex);
			String content = cell.getContents();
			if (content != null && !content.trim().equals("")) {
				return true;
			}
		}
		return flag;
	}

	public static List<Cell> findTables(Sheet s) {
		List<Cell> list = new ArrayList<Cell>();
		int curRowIndex = 1;
		int curColIndex = 0;
		int rows = s.getRows();
		int cols = s.getColumns();
		while (curRowIndex < s.getRows()) {
			Cell cell = s.getCell(1, curRowIndex);
			Cell a = s.getCell(1, 2);
			String content = cell.getContents();
			boolean cellFlag = (content != null && !content.trim().equals(""));
			boolean rowFlag = isTableRow(s, curRowIndex, cols);
			if (cellFlag || rowFlag) {
				Cell aCell = s.getCell(1, curRowIndex - 1);
				content = aCell.getContents();
				if ((ExcelConfig.isBlank(cell) || content.trim().equals(""))) {
					content = null;
				}
				if (content == null || content.trim().equals("")) {
					list.add(cell);

				}
			}
			curRowIndex++;
		}
		return list;
	}

	/**
	 * 取得格式化数据
	 */
	protected ExcelTable readFormatTable() {
		ExcelTable aExcelTable = new ExcelTable();
		
		// 二维表
//		aTableExcelModel = new TableExcelModel(aTableModel,	2, 0, -1, 4);
//		aTableExcelModel.extract();
//		aExcelTable.aTableExcelModel = aTableExcelModel;
		aExcelTable.aTableModel = aTableModel;		
		aExcelTable.name = aTableModel.name;
//		
//		aExcelTable.data = aTableExcelModel.getData();
//		// 属性表
//		String[] names = new String[] { "SQL" };
//		AttributeExcelModel aAttributeExcelModel = new AttributeExcelModel(
//				aTableModel, names);
//		aAttributeExcelModel.extract();
////		table.setSQL(aAttributeExcelModel.getValue("SQL"));
////		table.setName(aAttributeExcelModel.getValue("名称"));
		
		return aExcelTable;
		
	}

	private void writeAllTable(String filePath, Sheet s) throws BiffException,
			IOException, SameNameException {

		AllTable allTable = Config.allTable;
		List<Cell> list = findTables(s);
		Cell dateCell = s.getCell(0, 0);
		String aDate = dateCell.getContents();
		for (int m = 0; m < list.size(); m++) {
			Cell aCell = list.get(m);
			aTableModel = writeTable(aCell, s);
			ExcelTable table = readFormatTable();
			allTable.putExcelTable(table.name, table);
		}
	}

	public static int getRowNum(Sheet s, Cell aCell) {
		int row = s.getRows();
		int i = 0;
		for (i = aCell.getRow(); i < s.getRows(); i++) {
			Cell cell = s.getCell(2, i);
			String content = cell.getContents();
			if (!(ExcelConfig.isBlank(cell))) {
				if (content == null || content.trim().equals("")) {
					row = i - aCell.getRow();
					break;
				}
			}
		}
		if (i == s.getRows()) {
			row = i - aCell.getRow();
		}
		return row;
	}

	public int getColNum(Sheet s, Cell aCell) {
		int columns = s.getColumns();
		int column = 0;
		for (int j = 0; j < s.getColumns(); j++) {
			Cell cell = s.getCell(j, aCell.getRow());
			Range range = rangeMap.get(cell.getRow() + "_" + cell.getColumn());
			int colSpan = 1;
			if (range != null) {
				colSpan = range.getBottomRight().getColumn()
						- range.getTopLeft().getColumn() + 1;
				column += colSpan;
			} else {
				column += colSpan;
			}
			String content = cell.getContents();

			if (content == null || content.trim().equals("")) {
				column--;
				break;
			}

		}

		Cell cell = s.getCell(2, 2);

		return column;
	}

	/**
	 * 构建查询树
	 * 
	 */
	public void getQueryTree() {

		return;
	}

	/**
	 * 根据查询树构建SQL
	 */

	public String getSQL() {

		return null;
	}

}
